package com.personal.ifanya.infrastructure.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailBoxService {

	
	public static String emailSenderAddress = "tttbbb222000@163.com";
	
	public static class MyAuthenticator extends Authenticator{
		    String userName=null;   
		    String password=null;   
		        
		    public MyAuthenticator(){   
		    }   
		    public MyAuthenticator(String username, String password) {    
		        this.userName = username;    
		        this.password = password;    
		    }    
		    protected PasswordAuthentication getPasswordAuthentication(){   
		        return new PasswordAuthentication(userName, password);   
		    }   
	}
	
	public static boolean sendTextConfirmEmail(String toReceiverAddress, String textMessage)
	{
		// �ж��Ƿ���Ҫ�����֤    
		  MyAuthenticator authenticator = null;    
	      
	      authenticator = new MyAuthenticator(emailSenderAddress, "wmsjtsrj");    
  
	      Properties pro = new Properties();    
	      // configure smtp
	      pro.put("mail.smtp.host", "smtp.163.com");    
	      pro.put("mail.smtp.port", "25");    
	      pro.put("mail.smtp.auth", "true");    
 
	      // ����ʼ��Ự���Ժ�������֤������һ�������ʼ���session    
	      Session sendMailSession = Session.getDefaultInstance(pro,authenticator);    
	      try {    
	          // ���session����һ���ʼ���Ϣ    
	          Message mailMessage = new MimeMessage(sendMailSession);    
	          // �����ʼ������ߵ�ַ    
	          Address from = new InternetAddress(emailSenderAddress );    
		      // �����ʼ���Ϣ�ķ�����    
		      mailMessage.setFrom(from);    
		      // �����ʼ��Ľ����ߵ�ַ�������õ��ʼ���Ϣ��    
		      Address to = new InternetAddress(toReceiverAddress);    
		      mailMessage.setRecipient(Message.RecipientType.TO,to);    
		      // �����ʼ���Ϣ������    
		      mailMessage.setSubject("Hello world!!!");    
		      // �����ʼ���Ϣ���͵�ʱ��    
		      mailMessage.setSentDate(new Date());    
		      // �����ʼ���Ϣ����Ҫ����    
		      String mailContent = textMessage;    
		      
		      mailMessage.setText(mailContent);    
		      // �����ʼ�    
		      Transport.send(mailMessage);  

		      return true;    
	      } catch (MessagingException ex) {    
	       
	    	  ex.printStackTrace();    
	      }    
	      return false;    
		
	}
	
	
	public static boolean sendHtmlConfirmEmail(String toReceiverAddress, String htmlMessage)
	{
		// �ж��Ƿ���Ҫ�����֤    
		  MyAuthenticator authenticator = null;    
	      
	      authenticator = new MyAuthenticator(emailSenderAddress, "wmsjtsrj");    

	      Properties pro = new Properties();    
	      // configure smtp
	      pro.put("mail.smtp.host", "smtp.163.com");    
	      pro.put("mail.smtp.port", "25");    
	      pro.put("mail.smtp.auth", "true");    

	      // ����ʼ��Ự���Ժ�������֤������һ�������ʼ���session    
	      Session sendMailSession = Session.getDefaultInstance(pro,authenticator);    
	      try {    
	          // ���session����һ���ʼ���Ϣ    
	          Message mailMessage = new MimeMessage(sendMailSession);    
	          // �����ʼ������ߵ�ַ    
	          Address from = new InternetAddress(emailSenderAddress );    
		      // �����ʼ���Ϣ�ķ�����    
		      mailMessage.setFrom(from);    
		      // �����ʼ��Ľ����ߵ�ַ�������õ��ʼ���Ϣ��    
		      Address to = new InternetAddress(toReceiverAddress);    
		      mailMessage.setRecipient(Message.RecipientType.TO,to);    
		      // �����ʼ���Ϣ������    
		      mailMessage.setSubject("��٢һ��������֤.");    
		      // �����ʼ���Ϣ���͵�ʱ��    
		      mailMessage.setSentDate(new Date());     
		      
			  // MiniMultipart����һ�������࣬��MimeBodyPart���͵Ķ���    
		      Multipart mainPart = new MimeMultipart();    
		      // ����һ����HTML���ݵ�MimeBodyPart    
		      BodyPart html = new MimeBodyPart();    
		      // ����HTML����    
		      html.setContent( htmlMessage, "text/html; charset=utf-8");    
		      mainPart.addBodyPart(html);    
		      // ��MiniMultipart��������Ϊ�ʼ�����    
		      mailMessage.setContent(mainPart);    
		      // �����ʼ�    
		      Transport.send(mailMessage);    
		        
		      return true;    
	      } catch (MessagingException ex) {    
	       
	    	  ex.printStackTrace();    
	      }    
	      return false;    
		
		
		

		
	}
	
	
	public static void main(String[] args) {
		
		//sendTextConfirmEmail("542561233@qq.com");
		sendHtmlConfirmEmail("542561233@qq.com","Hello World.");
		
		System.out.println("yes!");
	}
}
